/**
 * 
 */
package com.yerba.ricecooker.indicator.technical;

import com.yerba.ricecooker.indicator.generic.Indicator;
import com.yerba.ricecooker.indicator.generic.IndicatorData1;
import com.yerba.ricecooker.quote.Quotes;

/**
 * @author mxie
 * 
 */
public class EMA extends Indicator<IndicatorData1>
{
    // constructor
    public EMA(Quotes quotes, int periods)
    {
        super(quotes);
        this.periods = periods;

        calculate();
    }
    
    public String name(){
        return "EMA(" + getQuotes().getSymbol().trim() + "," + getPeriods() + ")";
    }

    // indicator parameters
    private int periods = 99;

    public int getPeriods()
    {
        return periods;
    }

    public void setPeriods(int periods)
    {
        this.periods = periods;
    }

    protected void add(String date, double value1)
    {
        IndicatorData1 value = new IndicatorData1(value1);
        super.add(date, value);
    }

    public void calculate()
    {
        clear();

        if (getQuotes().size() < getPeriods()) { return; }

        double value = 0;
        double ema_seed;
        double multi = (double) 2 / (double) (1 + getPeriods());

        ema_seed = 0;

        int i = 0;
        for (; i < getPeriods(); i++)
        {
            ema_seed += getQuotes().get(i).getAdjclose();
        }

        ema_seed /= (double) getPeriods();

        for (; i < getQuotes().size(); i++)
        {
            value = multi * (getQuotes().get(i).getAdjclose() - ema_seed) + ema_seed;
            ema_seed = value;
            add(getQuotes().get(i).getDate(), value);
        }
    }

}
